package com.example.candiates.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.candiates.domain.InterviewQuestion;
import com.example.candiates.mapper.InterviewQuestionMapper;
import com.example.candiates.service.InterviewQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InterviewQuestionServiceImpl implements InterviewQuestionService {
    
    @Autowired
    private InterviewQuestionMapper interviewQuestionMapper;
    
    @Override
    public List<InterviewQuestion> getQuestionsBySetId(Integer questionSetId) {
        try {
            LambdaQueryWrapper<InterviewQuestion> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(InterviewQuestion::getQuestionSetId, questionSetId)
                  .eq(InterviewQuestion::getStatus, 1)  // 只获取启用的题目
                  .orderByAsc(InterviewQuestion::getOrderNum);  // 按排序号升序
            return interviewQuestionMapper.selectList(wrapper);
        } catch (Exception e) {
            System.err.println("获取题目列表失败: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<InterviewQuestion> getQuestionsBySetIds(String questionSetIds) {
        try {
            // 清理题目集ID字符串，移除括号和引号
            String cleanIds = questionSetIds.replaceAll("[\\[\\]\"]", "");
            return interviewQuestionMapper.selectByQuestionSetIds(cleanIds);
        } catch (Exception e) {
            System.err.println("解析题目集ID失败: " + e.getMessage());
            return new ArrayList<>();
        }
    }
} 